###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/ModifyCategory.pm.pl -> ModifyCategory module           #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::ModifyCategory;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, error => "", @_);

  my $LANGUAGE = {
    "MISSING" => {
      "TITLE"       => qq~<li>You didn't fill in the "Title" field.~,
      "ROOT"        => qq~<li>You didn't fill in the "Location" field.~,
      "STYLESHEET"  => qq~<li>You didn't fill in the "StyleSheet" field.~,
      "LANGUAGESET" => qq~<li>You didn't fill in the "Language Set" field.~,
      "TEMPLATESET" => qq~<li>You didn't fill in the "Template Set" field.~
    },
    "TOOLONG" => {
      "TITLE"       => qq~<li>The value for the "Title" field must be 128 characters or less.~,
      "DESCRIPTION" => qq~<li>The value for the "Description" field must be 512 characters or less.~,
      "IMAGE"       => qq~<li>The value for the "Image" field must be 512 characters or less.~
    },
    "INVALID" => {
      "ROOT"        => qq~<li>The value you entered for the "Location" field is invalid.~,
      "STYLESHEET"  => qq~<li>The value you entered for the "StyleSheet" field is invalid.~,
      "LANGUAGESET" => qq~<li>The value you entered for the "Language Set" field is invalid.~,
      "TEMPLATESET" => qq~<li>The value you entered for the "Template Set" field is invalid.~
    },
    "ERROR"   => qq~<p><font class="error">There were errors:<ul>[%error%]</ul></font>~
  };

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "Please fill out all the required (<font class=\"form-required\"><b>*</b></font>) fields:".$in{'error'}, colspan => "2" }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'CATEGORY'}->{'ID'},
    subject   => "ID"
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_TITLE",
    value     => $in{'input'}->{'CATEGORY'}->{'TITLE'},
    subject   => "Title",
    required  => 1
  );
  
  $Body .= $Dialog->FormTextArea(
    name      => "FORM_DESCRIPTION",
    value     => $in{'input'}->{'CATEGORY'}->{'DESCRIPTION'},
    subject   => "Description",
    rows      => 3
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_IMAGE",
    value     => $in{'input'}->{'CATEGORY'}->{'IMAGE'},
    subject   => "Image"
  );

  my @options;
  foreach my $category (@{ $in{'input'}->{'CATEGORIES'} }) {
    next if ($category->{'ID'} == $in{'input'}->{'CATEGORY'}->{'ID'});

    my @root = split(/,/, $category->{'ROOT'});
    my $found;
    foreach my $root (@root) {
      $found = 1 and last if ($root == $in{'input'}->{'CATEGORY'}->{'ID'});
    }

    next if ($found);

    push(@options, { value => $category->{'ID'}, label => "/ ".$category->{'FULL_TITLE'} });
  }

  unshift(@options, { value => "0", label => "/" });

  my @root = split(/,/, $in{'input'}->{'CATEGORY'}->{'ROOT'});
  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_ROOT",
    value     => $root[$#root],
    subject   => "Location",
    required  => 1,
    items     => \@options
  );

  $Body .= $Dialog->FormCheckBox(
    name      => "FORM_USER_ADD",
    value     => $in{'input'}->{'CATEGORY'}->{'USER_ADD'},
    items     => [{ value => "1", label => "Allow users to add resources to this category?" }]
  );

  @options = &Standard::MapArray({ value => "\${ID}", label => "\${DESCRIPTION}" }, $in{'input'}->{'SKINS'});
  unshift(@options, { value => "[CUSTOM]", label => "(Custom skin)" });
  unshift(@options, { value => "", label => "(Global default skin specified in General Options)" });

  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_SKIN",
    value     => ($in{'input'}->{'CATEGORY'}->{'skin'}->{'CUSTOM'} ? "[CUSTOM]" : $in{'input'}->{'CATEGORY'}->{'skin'}->{'ID'}),
    subject   => "Skin",
    required  => 1,
    items     => \@options,
    extra     => { onChange => "form.action.value='ModifyCategory';form.submit();" }
  );
  
  my $Skin = (defined($SL::QUERY{'FORM_SKIN'}) ? $SL::QUERY{'FORM_SKIN'} : ($in{'input'}->{'CATEGORY'}->{'skin'}->{'CUSTOM'} ? "[CUSTOM]" : $in{'input'}->{'CATEGORY'}->{'skin'}->{'ID'}));
  if ($Skin eq "[CUSTOM]") {
    @options = &Standard::MapArray({ value => "\${ID}", label => "\${DESCRIPTION}" }, $in{'input'}->{'STYLESHEETS'});

    $Body .= $Dialog->FormSelectBox(
      name      => "FORM_STYLESHEET",
      value     => $in{'input'}->{'CATEGORY'}->{'skin'}->{'STYLESHEET'},
      subject   => "StyleSheet",
      required  => 1,
      items     => \@options
    );

    @options = &Standard::MapArray({ value => "\${ID}", label => "\${DESCRIPTION}" }, $in{'input'}->{'LANGUAGESETS'});

    $Body .= $Dialog->FormSelectBox(
      name      => "FORM_LANGUAGESET",
      value     => $in{'input'}->{'CATEGORY'}->{'skin'}->{'LANGUAGESET'},
      subject   => "Language Set",
      required  => 1,
      items     => \@options
    );

    @options = &Standard::MapArray({ value => "\${ID}", label => "\${DESCRIPTION}" }, $in{'input'}->{'TEMPLATESETS'});

    $Body .= $Dialog->FormSelectBox(
      name      => "FORM_TEMPLATESET",
      value     => $in{'input'}->{'CATEGORY'}->{'skin'}->{'TEMPLATESET'},
      subject   => "Template Set",
      required  => 1,
      items     => \@options
    );
  }

  if (scalar(@{ $GENERAL->{'CATEGORY_EXTRA'} }) >= 1) {
    $Body .= $Dialog->TableHeader(titles => [{ title => "Extra", align => "left", colspan => 2 }]);
  
    $in{'input'}->{'CATEGORY'}->{'extra'} = eval($in{'input'}->{'CATEGORY'}->{'EXTRA'});  
    foreach my $field (@{ $GENERAL->{'CATEGORY_EXTRA'} }) {
      $Body .= $Dialog->FormTextBox(
        name      => "FORM_EXTRA_".$field->{'ID'},
        value     => $in{'input'}->{'CATEGORY'}->{'extra'}->{$field->{'ID'}},
        subject   => $field->{'DESCRIPTION'}
      );
    }
  }

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Modify" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Modify a Category",
    form    => 1,
    hiddens => [
      { name => "Referer", value => $SL::QUERY{'Referer'} },
      { name => "CID", value => $in{'input'}->{'CATEGORY'}->{'ID'} },
      { name => "action", value => "DoModifyCategory" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ],
    toolbar => [
      { type => "divider" },
      { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?action=Category&CID=$in{'input'}->{'CATEGORY'}->{'ID'}", target => "_blank", icon => "Toolbar/view.gif", caption => "View" },
      { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=RemoveCategories&CID=$in{'input'}->{'CATEGORY'}->{'ID'}&Referer=".$SL::CGI->escape($SL::QUERY{'Referer'})."&SID=$SL::ADMIN{'SID'}", icon => "Toolbar/remove.gif", caption => "Remove" },
      { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Resources&OneLevel=1&Root=$in{'input'}->{'CATEGORY'}->{'ID'}&SID=$SL::ADMIN{'SID'}", icon => "Toolbar/resources.gif", caption => "Resources" }
    ]
  );

  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "The following category has been modified:", colspan => 2 }]);

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'CATEGORY'}->{'ID'},
    subject   => "ID"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'TITLE'},
    subject   => "Title"
  );
  
  $Body .= $Dialog->FormTextArea(
    value     => $in{'input'}->{'RECORD'}->{'DESCRIPTION'},
    subject   => "Description"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $in{'input'}->{'RECORD'}->{'IMAGE'},
    subject   => "Image"
  );

  $Body .= $Dialog->FormTextBox(
    value     => ($in{'input'}->{'RECORD'}->{'ROOT'} eq "0" ? "/" : $in{'input'}->{'ROOT_CATEGORY'}->{'FULL_TITLE'}),
    subject   => "Location"
  );

  if ($in{'input'}->{'RECORD'}->{'skin'}->{'ID'}) {
    $Body .= $Dialog->FormTextBox(
      value     => $in{'input'}->{'SKIN'}->{'DESCRIPTION'},
      subject   => "Skin"
    );
  } elsif ($in{'input'}->{'RECORD'}->{'skin'}->{'CUSTOM'}) {
    $Body .= $Dialog->FormTextBox(
      value     => "(Custom skin)",
      subject   => "Skin"
    );
    $Body .= $Dialog->FormTextBox(
      value     => $in{'input'}->{'STYLESHEET'}->{'DESCRIPTION'},
      subject   => "StyleSheet"
    );
    $Body .= $Dialog->FormTextBox(
      value     => $in{'input'}->{'LANGUAGESET'}->{'DESCRIPTION'},
      subject   => "Language Set"
    );
    $Body .= $Dialog->FormTextBox(
      value     => $in{'input'}->{'TEMPLATESET'}->{'DESCRIPTION'},
      subject   => "Template Set"
    );
  } else {
    $Body .= $Dialog->FormTextBox(
      value     => "(Global default skin specified in General Options)",
      subject   => "Skin"
    );
  }

  if (scalar(@{ $GENERAL->{'CATEGORY_EXTRA'} }) >= 1) {
    $Body .= $Dialog->TableHeader(titles => [{ title => "Extra", align => "left", colspan => 2 }]);
  
    foreach my $field (@{ $GENERAL->{'CATEGORY_EXTRA'} }) {
      $Body .= $Dialog->FormTextBox(
        value     => $in{'input'}->{'RECORD'}->{'extra'}->{$field->{'ID'}},
        subject   => $field->{'DESCRIPTION'}
      );
    }
  }

  $Body .= $Dialog->TableDivider(colspan => 2);
  $Body .= $Dialog->TableRow(items => [{ body => qq~Click <a href="~.($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Categories&SID=$SL::ADMIN{'SID'}").qq~">here</a> if you're not redirected in 5 seconds.~, colspan => 2 }]);

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Modify a Category",
    metas   => [
      { http_equiv => "refresh", content => "5; url=".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Categories&SID=$SL::ADMIN{'SID'}") }
    ]
  );
  
  return $Body;
}

1;